package com.stox.ui.javafx.charting.widget.axis;

import javafx.geometry.Side;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import com.stox.model.preference.Preference;
import com.stox.ui.javafx.charting.model.plot.AbstractPlot;
import com.stox.ui.javafx.charting.widget.Chart;

public abstract class AbstractAxis<T extends Chart<?>> extends Pane{

	private T chart;
	
	private Side side;
	
	protected Canvas canvas = new Canvas();
	
	protected GraphicsContext gc = canvas.getGraphicsContext2D();
	
	public AbstractAxis() {
		gc.setLineWidth(1);
		gc.setStroke(Color.WHITE);
		gc.setFont(Font.font("Verdana", FontWeight.EXTRA_LIGHT, 10));
		
		getChildren().add(canvas);
		canvas.widthProperty().bind(widthProperty());
		canvas.heightProperty().bind(heightProperty());
	}
	
	public void setHeight(double height) {
		super.setHeight(height);
	}

	public void update() {
		gc.setFill(Preference.instance.getChartingPreference().getDefaultBackgroundColor());
		gc.fillRect(0, 0, getWidth(), getHeight());
	}
	
	private void positionAxis() {
		if(null == side || null == chart) return;
		switch(side) {
		case LEFT : chart.setLeft(this);break;
		case TOP : chart.setTop(this); break;
		case RIGHT : chart.setRight(this); break;
		case BOTTOM : chart.setBottom(this); break;
		}
	}
	
	public T getChart() {
		return chart;
	}

	public void setChart(T chart) {
		this.chart = chart;
		positionAxis();
	}
	
	public AbstractPlot<?,?,?,?> getTargetPlot() {
		return getChart().getFrontPlot();
	}

	public Side getSide() {
		return side;
	}

	public void setSide(Side side) {
		this.side = side;
		positionAxis();
	}

}
